package cvi.com.reader.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.adapter.ViewPagerAdatper;
import cvi.com.reader.base.BaseActivity;
import cvi.com.reader.tool.DepthPageTransformer;
import cvi.com.reader.tool.UtilsOnclickListen;

/**
 * Created by 梅涛 on 2020/8/18.
 * 导航页
 */
public class GuideActivity extends BaseActivity {
    private int mDistance;
    private ImageView mOne_dot;
    private ImageView mTwo_dot;
    private ImageView mThree_dot;
    @BindView(R.id.in_viewpager)
    ViewPager inViewpager;
    @BindView(R.id.in_ll)
    LinearLayout inLl;
    @BindView(R.id.iv_light_dots)
    ImageView ivLightDots;
    @BindView(R.id.rl_dots)
    RelativeLayout rlDots;
    @BindView(R.id.go_read)
    Button goRead;
    private List<View> mViewList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_guide;
    }

    @Override
    protected void initView() {
        mViewList = new ArrayList<View>();
        LayoutInflater lf = getLayoutInflater().from(GuideActivity.this);
        View view1 = lf.inflate(R.layout.guide_view1, null);
        View view2 = lf.inflate(R.layout.guide_view2, null);
        View view3 = lf.inflate(R.layout.guide_view3, null);
        mViewList.add(view1);
        mViewList.add(view2);
        mViewList.add(view3);
        inViewpager.setAdapter(new ViewPagerAdatper(mViewList));
        addDots();
        moveDots();
        inViewpager.setPageTransformer(true, new DepthPageTransformer());
        mOne_dot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UtilsOnclickListen.isFastClick()) {
                    inViewpager.setCurrentItem(0);
                }
            }
        });
        mTwo_dot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UtilsOnclickListen.isFastClick()) {
                    inViewpager.setCurrentItem(1);
                }
            }
        });
        mThree_dot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UtilsOnclickListen.isFastClick()) {
                    inViewpager.setCurrentItem(2);
                }
            }
        });
        goRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UtilsOnclickListen.isFastClick()) {
                    jump(LoginActivityNew.class);
                    finish();
                }
            }
        });
    }

    private void addDots() {
        mOne_dot = new ImageView(this);
        mOne_dot.setImageResource(R.drawable.gray_dot);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, 0, 40, 0);
        inLl.addView(mOne_dot, layoutParams);
        mTwo_dot = new ImageView(this);
        mTwo_dot.setImageResource(R.drawable.gray_dot);
        inLl.addView(mTwo_dot, layoutParams);
        mThree_dot = new ImageView(this);
        mThree_dot.setImageResource(R.drawable.gray_dot);
        inLl.addView(mThree_dot, layoutParams);
    }

    private void moveDots() {
        ivLightDots.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //获得两个圆点之间的距离
                mDistance = inLl.getChildAt(1).getLeft() - inLl.getChildAt(0).getLeft();
                ivLightDots.getViewTreeObserver()
                        .removeGlobalOnLayoutListener(this);
            }
        });
        inViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //页面滚动时小白点移动的距离，并通过setLayoutParams(params)不断更新其位置
                float leftMargin = mDistance * (position + positionOffset);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivLightDots.getLayoutParams();
                params.leftMargin = (int) leftMargin;
                ivLightDots.setLayoutParams(params);
              /*  if(position==2){
                    mBtn_next.setVisibility(View.VISIBLE);
                }
                if(position!=2&&mBtn_next.getVisibility()==View.VISIBLE){
                    mBtn_next.setVisibility(View.GONE);
                }*/
            }

            @Override
            public void onPageSelected(int position) {
                //页面跳转时，设置小圆点的margin
                float leftMargin = mDistance * position;
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivLightDots.getLayoutParams();
                params.leftMargin = (int) leftMargin;
                ivLightDots.setLayoutParams(params);
             /*   if(position==2){
                    mBtn_next.setVisibility(View.VISIBLE);
                }
                if(position!=2&&mBtn_next.getVisibility()==View.VISIBLE){
                    mBtn_next.setVisibility(View.GONE);
                }*/
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void extrasDoSome(Bundle extras) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
